package com.application.merecipe.client;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.application.merecipe.shared.Recipe;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

public class RecipesComposite extends Composite {
	private RecipeServiceAsync asyncService = GWT.create(RecipeService.class);
	private List<Recipe> Recipes = new ArrayList<Recipe>();
	private DecoratorPanel decoratorPanel = new DecoratorPanel();
	private VerticalPanel verticalPanel = new VerticalPanel();
	private CellTable<Recipe> cellTableRecipes = new CellTable<Recipe>();
	private Label labelUser = new Label();

	private TextColumn<Recipe> colRecipeName = new TextColumn<Recipe>() {
		@Override
		public String getValue(Recipe Recipe) {
			return Recipe.getTitle();
		}
	};
	private final HorizontalPanel horizontalPanel = new HorizontalPanel();
	private final Label lblRecipes = new Label("Recipes");
	private final ScrollPanel scrollPanel = new ScrollPanel();

	public RecipesComposite() {
		decoratorPanel.setWidget(verticalPanel);
		horizontalPanel.setSpacing(10);

		Button btnNew = new Button("New");
		verticalPanel.add(btnNew);
		btnNew.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				createNewRecipe();
			}
		});

		verticalPanel.add(horizontalPanel);
		lblRecipes.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		horizontalPanel.add(lblRecipes);
		verticalPanel.add(labelUser);

		verticalPanel.add(scrollPanel);
		scrollPanel.setWidget(cellTableRecipes);
		scrollPanel.setSize("1000px", "300px");
		cellTableRecipes.setPageSize(1000);
		cellTableRecipes.setSize("100%", "100%");

		cellTableRecipes.addColumn(colRecipeName, "Name");

		final SingleSelectionModel<Recipe> selectionModel = new SingleSelectionModel<Recipe>();
		selectionModel
				.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
					public void onSelectionChange(SelectionChangeEvent event) {
						removeRecipeComposites();
						Recipe recipe = (Recipe) selectionModel
								.getSelectedObject();
						RootPanel.get("merecipeContainer").add(
								new RecipeComposite(recipe));

					}
				});

		cellTableRecipes.setSelectionModel(selectionModel);
		initWidget(decoratorPanel);

		loadRecipes();
	}

	public void loadRecipes() {
		loadRecipes(null);
	}

	public void loadRecipes(final Recipe Recipe) {
		asyncService.loadRecipes(null, new AsyncCallback<List<Recipe>>() {

			@Override
			public void onSuccess(List<Recipe> result) {
				Recipes = result;
				if (Recipes != null) {
					cellTableRecipes.setRowData(0, Recipes);
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				RootPanel.get("infoContainer").clear();
				RootPanel.get("infoContainer").add(
						new Label(caught.getMessage()));
			}
		});

	}

	private void createNewRecipe() {
		removeRecipeComposites();
		Recipe recipe = new Recipe();
		RootPanel.get("merecipeContainer").add(new RecipeComposite(recipe));
	}

	private void removeRecipeComposites() {
		RootPanel.get("infoContainer").clear();
		Iterator<Widget> iterator = RootPanel.get("merecipeContainer")
				.iterator();
		Widget widget;
		while (iterator.hasNext()) {
			widget = iterator.next();
			if (widget instanceof RecipeComposite) {
				RootPanel.get("merecipeContainer").remove(widget);
			}
		}
	}

}
